package com.km.core.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.base.Strings;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;
import java.util.Map;

/**
 * <p>日志实体类</p>
 * Created by zhezhiyong@163.com on 2017/4/26.
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class LogResult {
    public static final String LABEL_NORMAL = "normal";
    //    private Date request_time = new Date();
//    private String request_id = UUID.randomUUID().toString().replaceAll("-", "");
//    private String project = "dubbo";
//    private String model = "dubbo";
//    private String protocol;
    private String parameter;
    private String more_info;
    private String label;
//    private String label = "km";
//    private int status = 0;

    private String url;
    private Map<String, String> header;
    private String method;
    private String response;
    private Map<String, String> requestHeaders;
    private Map<String, String> responseHeaders;
    @JSONField(serialize = false)
    private Date start;
    @JSONField(serialize = false)
    private Date end;
    private float response_time;

    public LogResult(String label, String more_info) {
        this.label = label;
        this.more_info = more_info;
    }

    public LogResult(String more_info) {
        this.label = LABEL_NORMAL;
        this.more_info = more_info;
    }

    public void setParameter(String parameter) {
        //  todo: sendAdSms重复
        if (Strings.isNullOrEmpty(this.parameter)) {
            this.parameter = parameter;
        } else {
            this.parameter += parameter;
        }
    }

    private void calculate() {
        end = new Date();
        if (null == start) return;
        this.response_time = (end.getTime() - start.getTime()) / 1000f;
    }

    @Override
    public String toString() {
        calculate();
        return JSON.toJSONString(this);
    }
}
